package day24;

import java.io.*;
import java.util.Date;

public class Student implements Serializable {
    private static final long serialVersionUID = 12345678L;
    //注2：序列化  可以设置  指定字段  忽略序列化
    //通过static 修饰的属性可以  因为它属于类
    //transient也可以忽略
    //注3：序列化和反序列化生成的文件  不要随便改  否则文件损坏
    Integer id;
    static String name;
    transient double score;
    Date birthday;
    StudentInfo info;
    public Student(Integer id, String name, double score, Date birthday, StudentInfo info) {
        this.id = id;
        this.name = name;
        this.score = score;
        this.birthday = birthday;
        this.info = info;
    }

    @Override
    public String toString() {
        return id + " " + name + " " + score + " " + birthday+ " " + info.toString();
    }
}

class StudentInfo implements Serializable {
    private static final long serialVersionUID = 12345678L;//本类不要，要删除
    Integer id;
    String sex;
    String phone;

    public StudentInfo(Integer id, String sex, String phone) {
        this.id = id;
        this.sex = sex;
        this.phone = phone;
    }
    @Override
    public String toString() {
        return id + " " + sex + " " + phone;
    }
}


class Demo11{
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //1.序列化  生成对象到本地文件
        //output();
        input();
        //2.反序列化  通过本地文件  生成java中的对象
    }
    public static void input() throws IOException, ClassNotFoundException {//反序列化
        FileInputStream fis = new FileInputStream("d://a//student.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Student s2 = (Student)ois.readObject();
        System.out.println(s2);
        ois.close();
        fis.close();
    }
    public static void output() throws IOException {//序列化
        Student s1 = new Student(10,"abc",99,new Date(),new StudentInfo(4,"男","100"));
        FileOutputStream fos = new FileOutputStream("d://a//student.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(s1);
        oos.close();
        fos.close();
    }
}
